import { Controller } from "../../mvc/core/Controller";
import { IRunBusinessController } from "./interfaces/IRunBusinessController";
import { IRunBusinessModule } from "../../../modules/interfaces/IRunBusinessModule";
import { RunBusinessModule } from "../../../modules/RunBusinessModule";
import { IRunBusinessSilverNoteModule } from "../../../modules/interfaces/IRunBusinessSilverNoteModule";
import { RunBusinessSilverNoteModule } from "../../../modules/RunBusinessSilverNoteModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SRunBusinessNpc } from "../../../Message/Public/SRunBusinessNpc";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SRunBusinessRefreshDt } from "../../../Message/Public/SRunBusinessRefreshDt";
import { ItemData } from "../resource/info/ItemData";
import { RunBusinessIntroWindow } from "../view/runBusiness/RunBusinessIntroWindow";
import { TaskInfo } from "../model/TaskInfo";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
type int = number;
//class RunBusinessController
    
    export  class RunBusinessController extends Controller implements IRunBusinessController
    {
       

        public get runBusinessModule(): IRunBusinessModule
        {
            if (this._runBusinessModule == null) 
            {
                this._runBusinessModule = new RunBusinessModule();
            }
            return this._runBusinessModule;
        }

        public get runBusinessSilverNoteModule(): IRunBusinessSilverNoteModule
        {
            if (this._runBusinessSilverNoteModule == null) 
            {
                this._runBusinessSilverNoteModule = new RunBusinessSilverNoteModule();
            }
            return this._runBusinessSilverNoteModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.UseTaskSilverNote, this.onUseTaskSilverNoteHandler);
            Dispatcher.addEventListener(EventName.RunBusinessCurrentNpc, this.onRunBusinessCurrentNpcHandler);
            Dispatcher.addEventListener(EventName.RunBusinessSellGoods, this.onRunBusinessSellGoodsHandler);
            Dispatcher.addEventListener(EventName.RunBusinessBuyGoods, this.onRunBusinessBuyGoodsHandler);
            Dispatcher.addEventListener(EventName.RunBusinessGetGoodsSuccess, this.onGetGoodsSuccessHandler);
            Dispatcher.addEventListener(EventName.RunBusinessRefreshTimeEnd, this.onRefreshTimeEndHandler);
            Dispatcher.addEventListener(EventName.RunBusinessRefreshTimeChange, this.onRefreshTimeChange);
            Dispatcher.addEventListener(EventName.RunBusinessSilverNoteChange, this.onSilverNoteChangeHandler);
            Dispatcher.addEventListener(EventName.RunBusinessIntroOpenOrClose, this.onIntroOpenOrCloseHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRunBusinessRefreshDt, this.onRunBusinessRefreshDtHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackRunBusinessPackChange, this.onRunBusinessPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetHandler);
            return;
        }

        /* internal  */onUseTaskSilverNoteHandler(arg1: DataEvent): void
        {
            if (this.runBusinessSilverNoteModule.isHide) 
            {
                this.runBusinessSilverNoteModule.show();
                NpcEffectRule.registDialogEffectWindow(this.runBusinessSilverNoteModule);
            }
            var loc1=cache.pack.runBusinessPackCache.getItemsByJudge();
            this.runBusinessSilverNoteModule.update(loc1);
            this.runBusinessSilverNoteModule.updateTime();
            return;
        }

        /* internal  */onRunBusinessTaskAcceptHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onRunBusinessCurrentNpcHandler(arg1: DataEvent): void
        {
            var loc1=cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupRunBusiness);
            if (loc1) 
            {
                this._npcId = arg1.data as int;
                GameProxy.runBusinessProxy.getRunBuessinessNpc(this._npcId);
                this.runBusinessModule.show();
                this.runBusinessModule.updateTime();
                NpcEffectRule.registDialogEffectWindow(this.runBusinessModule);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(35300));
            }
            return;
        }

        /* internal  */onGetGoodsSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SRunBusinessNpc;
            cache.runBusiness.runBusinessNpc = loc1;
            this.runBusinessModule.update(loc1);
            return;
        }

        /* internal  */onRunBusinessSellGoodsHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.runBusinessProxy.runBuessinessSellItem(this._npcId, loc1.uid, loc1.amount);
            return;
        }

        /* internal  */onRunBusinessBuyGoodsHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.runBusinessProxy.runBuessinessBuyItem(this._npcId, loc1.itemCode, loc1.amount);
            return;
        }

        /* internal  */onRunBusinessRefreshDtHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRunBusinessRefreshDt;
            cache.runBusiness.nextRefreshDate = loc1.nextRefreshDt;
            return;
        }

        /* internal  */onRunBusinessPackChangeHandler(arg1: Object): void
        {
            var loc1=null;
            if (!(this._runBusinessModule == null) && !this._runBusinessModule.isHide) 
            {
                this._runBusinessModule.update(cache.runBusiness.runBusinessNpc);
            }
            if (!(this._runBusinessSilverNoteModule == null) && !this._runBusinessSilverNoteModule.isHide) 
            {
                loc1 = cache.pack.runBusinessPackCache.getItemsByJudge();
                this._runBusinessSilverNoteModule.update(loc1);
            }
            return;
        }

        /* internal  */onRefreshTimeEndHandler(arg1: DataEvent): void
        {
            if (!(this._runBusinessModule == null) && !this._runBusinessModule.isHide) 
            {
                GameProxy.runBusinessProxy.getRunBuessinessNpc(this._npcId);
            }
            return;
        }

        /* internal  */onRefreshTimeChange(arg1: DataEvent): void
        {
            if (!(this._runBusinessModule == null) && !this._runBusinessModule.isHide) 
            {
                this._runBusinessModule.updateTime();
            }
            if (!(this._runBusinessSilverNoteModule == null) && !this._runBusinessSilverNoteModule.isHide) 
            {
                this._runBusinessSilverNoteModule.updateTime();
            }
            return;
        }

        /* internal  */onSilverNoteChangeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (!(this._runBusinessModule == null) && !this._runBusinessModule.isHide) 
            {
                this._runBusinessModule.setMoney(loc1);
            }
            if (!(this._runBusinessSilverNoteModule == null) && !this._runBusinessSilverNoteModule.isHide) 
            {
                this._runBusinessSilverNoteModule.setMoney(loc1);
            }
            return;
        }

        /* internal  */onIntroOpenOrCloseHandler(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=0;
            if (this._introWindow == null) 
            {
                this._introWindow = new RunBusinessIntroWindow();
            }
            if (this.runBusinessModule.isHide) 
            {
                if (!this.runBusinessSilverNoteModule.isHide) 
                {
                    loc1 = this.runBusinessSilverNoteModule.x + this.runBusinessSilverNoteModule.width;
                    loc2 = this.runBusinessSilverNoteModule.y + this.runBusinessSilverNoteModule.height - this._introWindow.height;
                }
            }
            else 
            {
                loc1 = this.runBusinessModule.x + this.runBusinessModule.width;
                loc2 = this.runBusinessModule.y + this.runBusinessModule.height - this._introWindow.height;
            }
            if (this._introWindow.isHide) 
            {
                this._introWindow.show(loc1, loc2);
            }
            else 
            {
                this._introWindow.hide();
            }
            return;
        }

        /* internal  */onTaskGetHandler(arg1: Object): void
        {
            if (arg1 == null || arg1.task == null) 
            {
                return;
            }
            var loc1=arg1.task as TaskInfo;
            if (loc1.sTask.group == ETaskGroup._ETaskGroupRunBusiness) 
            {
                this._checkCount = 0;
                flash.utils.setTimeout(this.onTaskGetLater, 400);
            }
            return;
        }

        /* internal  */onTaskGetLater(): void
        {
            var loc2=null;
            var loc1=cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropTaskSilverNote);
            if (loc1.length > 0) 
            {
                loc2 = loc1[0];
                Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, loc2));
            }
            else 
            {
                var loc3;
                var loc4=((loc3 = this)._checkCount + 1);
                loc3._checkCount = loc4;
                if (this._checkCount < 5) 
                {
                    flash.utils.setTimeout(this.onTaskGetLater, 400);
                }
            }
            return;
        }

        private /* var */_runBusinessModule: IRunBusinessModule;

        private /* var */_runBusinessSilverNoteModule: IRunBusinessSilverNoteModule;

        private /* var */_npcId: int=-1;

        private /* var */_introWindow: RunBusinessIntroWindow;

        private /* var */_checkCount: int;
    }
